// <copyright file="ConnectionForm.cs" company="DotNetSide">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>fabio.cozzolino</author>
// <date>30/06/2008 18.09.35</date>

namespace DotNetSide.SqlScriptGenerator
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DotNetSide.SqlScriptGenerator.Properties;
    using System.Net;

    public enum AuthenticationMethods
    {
        Windows = 0,
        SqlServer = 1
    }

    public partial class ConnectionForm : Form
    {
        private AuthenticationMethods authenticationMethods;

        public ConnectionForm()
        {
            InitializeComponent();

            authenticationMethods = AuthenticationMethods.Windows;
        }

        public string Server
        {
            get { return this.ServerComboBox.Text; }
            set { this.ServerComboBox.Text = value; }
        }

        public string Username
        {
            get { return this.UsernameTextBox.Text; }
            set { this.UsernameTextBox.Text = value; }
        }

        public string Password
        {
            get { return this.PasswordTextBox.Text; }
            set { this.PasswordTextBox.Text = value; }
        }

        public AuthenticationMethods AuthenticationMethod
        {
            get { return this.authenticationMethods; }
            set { this.authenticationMethods = value; }
        }

        private void ConnectButton_Click(object sender, EventArgs e)
        {
            if (this.ServerComboBox.Text == ".")
            {
                IPHostEntry host = Dns.GetHostEntry("localhost");
                this.ServerComboBox.Text = host.HostName;
            }

            if (!Settings.Default.HistoryServerList.Contains(this.ServerComboBox.Text))
            {
                Settings.Default.HistoryServerList.Add(this.ServerComboBox.Text);
            }
            Settings.Default.Save();
        }

        private void ConnectionForm_Load(object sender, EventArgs e)
        {
            if (Settings.Default.HistoryServerList == null)
            {
                Settings.Default.HistoryServerList = new System.Collections.Specialized.StringCollection();
            }

            foreach (string item in Settings.Default.HistoryServerList)
            {
                this.ServerComboBox.Items.Add(item);
            }

            this.AuthenticationComboBox.SelectedIndex = (int)authenticationMethods;
        }

        private void AuthenticationComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enable = false;
            if (this.AuthenticationComboBox.Text == "Sql Server Authentication")
            {
                enable = true;
                this.authenticationMethods = AuthenticationMethods.SqlServer;
            }
            else
            {
                this.authenticationMethods = AuthenticationMethods.Windows;
            }

            this.UsernameLabel.Enabled = enable;
            this.UsernameTextBox.Enabled = enable;
            this.PasswordLabel.Enabled = enable;
            this.PasswordTextBox.Enabled = enable;
        }
    }
}